version 18.0               // version control
set processors 8           // to ensure replicability across different numbers of cores
clear all                  // clear existing data
macro drop _all            // and macros, clean slate
set seed 20220909          // set seed
set scheme cleanplots      // clean plots

local pgm  "dst-Figure_5_balance_checks_observables_ols"             // file name
local who  "Muzhe Yang"                                              // author
local dte  "2025-01-20"                                              // created date
local dte2 "`c(current_date)'"                                       // last run date
local tag  "`pgm'.do, created by `who' on `dte', last run on `dte2'"

capture log close
log using "code\analysis\figures\\`pgm'.txt", text replace 
display "`tag'"

**# data prep ------------------------------------------------------------------------------------

use "data_clean\dst-data04_for_estimation_within_250_miles", clear
keep if wave == 2

*## (1) create the 9 cells, following page 215 of the following paper:
*## Giuntella, O. and F. Mazzonna (2019). "Sunset Time and the Economic Effects of Social Jetlag: Evidence from US Time Zone Borders." Journal of Health Economics 65: 210-226.

assert !missing(centroid_lat)
assert !missing(region)
gen     cell = 1 if centroid_lat > 40                        & region == "eastern and central"
replace cell = 2 if (34 < centroid_lat & centroid_lat <= 40) & region == "eastern and central"
replace cell = 3 if centroid_lat <= 34                       & region == "eastern and central"
replace cell = 4 if centroid_lat > 40                        & region == "central and mountain"
replace cell = 5 if (34 < centroid_lat & centroid_lat <= 40) & region == "central and mountain"
replace cell = 6 if centroid_lat <= 34                       & region == "central and mountain"
replace cell = 7 if centroid_lat > 40                        & region == "mountain and pacific"
replace cell = 8 if (34 < centroid_lat & centroid_lat <= 40) & region == "mountain and pacific"
replace cell = 9 if centroid_lat <= 34                       & region == "mountain and pacific"
tab cell time_zone, missing
summ centroid_lat if cell == 1 | cell == 4 | cell == 7
summ centroid_lat if cell == 2 | cell == 5 | cell == 8
summ centroid_lat if cell == 3 | cell == 6 | cell == 9

*## (2) observables

global x "pop white_pct black_pct hispanic_pct pop_under_18yr_pct pop_65yr_over_pct age_median educ_hs_pct educ_coll_pct married_pct hh_size hh_income_median home_value_median no_health_ins_pct unemploy_pct"
des $x

**# estimation prep -----------------------------------------------------------------------

encode CountyFIPS, gen(county_fips)
egen nomiss = rowmiss($x dist_to_border centroid_lat daylight_dur_max)
assert !missing(StateAbbr)
local radius = 50
gen sample_selection = (nomiss == 0 & StateAbbr != "AZ" & dist_to_border <= `radius')

**# figure ----------------------------------------------------------------------------------------------

foreach y of global x {
    areg `y' treat dist_to_border_signed centroid_lat if sample_selection == 1, absorb(cell) vce(cluster county_fips)
    estimates store `y'_all
    areg `y' treat dist_to_border_signed centroid_lat if sample_selection == 1 & region == "eastern and central", absorb(cell) vce(cluster county_fips)
    estimates store `y'_et_ct
}
estimates restore pop_all
local n_obs_all = e(N)
estimates restore pop_et_ct
local n_obs_et_ct = e(N)

coefplot (pop_all,   label("All time zones in the contiguous U.S.; sample size = `n_obs_all'")) ///
         (pop_et_ct, label("Eastern and central time zones; sample size = `n_obs_et_ct'")), bylabel("Population (in 1,000)") || ///
         (white_pct_all) ///
         (white_pct_et_ct), bylabel("% of population that is white") || ///
         (black_pct_all) ///
         (black_pct_et_ct), bylabel("% of population that is black") || ///
         (hispanic_pct_all) ///
         (hispanic_pct_et_ct), bylabel("% of population that is Hispanic or Latino") || ///
         (pop_under_18yr_pct_all) ///
         (pop_under_18yr_pct_et_ct), bylabel("% of population less than 18 years") || ///		 
         (pop_65yr_over_pct_all) ///
         (pop_65yr_over_pct_et_ct), bylabel("% of population 65 years or older") || ///
         (age_median_all) ///
         (age_median_et_ct), bylabel("Median age") || ///
         (educ_hs_pct_all) ///
         (educ_hs_pct_et_ct), bylabel("% of population aged 25+ with high school degree only") || ///
         (educ_coll_pct_all) ///
         (educ_coll_pct_et_ct), bylabel("% of population aged 25+ with bachelor's degree or higher") || ///
         (married_pct_all) ///
         (married_pct_et_ct), bylabel("% of population being married") || ///
         (hh_size_all) ///
         (hh_size_et_ct), bylabel("Average household size") || ///
         (hh_income_median_all) ///
         (hh_income_median_et_ct), bylabel("Median household income (in $1,000)") || ///
         (home_value_median_all) ///
         (home_value_median_et_ct), bylabel("Median home value (in $1,000)") || ///
         (no_health_ins_pct_all) ///
         (no_health_ins_pct_et_ct), bylabel("% of population without health insurance") || ///
         (unemploy_pct_all) ///
         (unemploy_pct_et_ct), bylabel("% of population being unemployed") || ///
         , keep(treat) ///
         coeflabels(treat = `""Treat (1/0):" "1 for census tracts" "located east of" "the time zone border;" "0 for census tracts" "located west of" "the time zone border""', labsize(*1)) ///
         mlabels mlabsize(*1) mlabcolor(black) mlabformat(%9.3f) mlabposition(12) mlabgap(*1) ///
         xline(0, lpattern(dash) lwidth(*1) lcolor(gray)) ///
         byopts(xrescale noiytick iscale(*0.7) cols(3)) ///
         levels(99 95 90) ciopts(lwidth(*3 *3 *3) lcolor(*.2 *.4 *.6)) ///
         legend(size(*0.6))

graph save "code\analysis\figures\\`pgm'", replace
graph export "code\analysis\figures\\`pgm'.png", replace
graph export "code\analysis\figures\\`pgm'.emf", replace

log close
exit